package FrameWork.World;

import org.apache.log4j.Logger;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;


public abstract class AbstractScene{

    protected Logger logger = Logger.getLogger(getClass());
    //场景名称
    private String name;
    //场景消息队列
    protected ConcurrentLinkedQueue<Runnable> messageQueue = new ConcurrentLinkedQueue<>();
    //场景是否正在运行
    protected volatile AtomicBoolean running = new AtomicBoolean(true);

    //心跳函数
    protected abstract void tick(long time);

    //TODO 添加过载控制
    protected void submit(Runnable task){
        if (this.running.get()){
            messageQueue.add(task);
        }else{
            logger.debug("场景已关闭，停止接受任务");
        }
    }


    protected void gracefullyStop(){
        this.running.set(false);
        for (Runnable task:messageQueue){
            task.run();
        }
    }

    protected boolean isRunning(){
        return running.get();
    }

    //停止线程
    protected void stop(){
        this.running.set(false);
        gracefullyStop();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
